//
//  FirstViewController.m
//  UI_07_delegate_block
//
//  Created by li cheng on 16/7/22.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "AppDelegate.h"

@interface FirstViewController ()<SecondVCPassValue>
@property (nonatomic, copy) NSString *titleTextFiled;
@end

@implementation FirstViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"%@", self.titleLabel.text);
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 1.
//    self.navigationController.navigationBar.translucent = NO;
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 60)];
    self.titleLabel.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.titleLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor yellowColor];
    button.frame = CGRectMake(100, 180, 100, 60);
    [self.view addSubview:button];
    [button addTarget:self action:@selector(pushNextVC:) forControlEvents:UIControlEventTouchUpInside];
    
    // \页面通信
    // 1. 属性传值,作为后一个页面的属性,并通过该属性进行一系列操作
    // 将FirstViewControlller的titleLabel.text传递给secondViewController的textfiled
    // FirstViewController -> 取到titleLabel.text
    //         pop ->               属性传值
    // SecondViewController -> titleString -> 去使用title做任意的事情
    // 2.协议传值(delegate)
    // 从后往前传值
    // second -> first
    // 决定要用代理的方式传值
    // 1. 决定要在second中声明协议 (最难确定)
    // 2. 声明deldegate @property id <Delefate名> deldegate;
    // 3. 在哪签订协议(只要可以拿到对象,就可以签订协议) xxx.delegate = self
    // 4. xxxdelegate = self; 会报黄,没签订协议,去签订的协议
    // 5. 签订完协议,会报黄,因为有必须实现的协议方法未实现,实现协议方法
    // 6.但是协议方法并没有执行,缺少触发时机
    // 在哪里触发该协议方法(加上对应的判断)
    
    
    // 3.关于navgation中控制器的释放
    // navigation中ViewControllers数组, 数组对对象有引用计数加一,当controller入栈时,则被加入数组中,应用技术+1
    // 只要不出栈,就不会被释放
    // A -> push -> B
    // B -> pop -> A B出栈, 对应释放
}


- (void)pushNextVC:(UIButton *)button{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.delegate = self;
    [self.navigationController pushViewController:secondVC animated:YES];
}

- (void)passValue:(NSString *)string{
    self.titleLabel.text = string;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
